﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace LeafBookmark.Data
{
    enum BookmarkSourceType
    {
        Unknown = 0,
        Explorer,
        FireFox,
        Chrome,
        Opera
    }

    class BookmarkLibrary
    {
        public IEnumerable<IBookmark> BookmarkList
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the type of the source.
        /// </summary>
        /// <value>The type of the source.</value>
        private BookmarkSourceType sourceType;
        public BookmarkSourceType SourceType
        {
            get
            {
                return sourceType;
            }

            private set
            {
                if(value != sourceType)
                {
                    sourceType = value;
                }
            }
        }

        public string RootPath
        {
            get;
            set;
        }

        public BookmarkLibrary()
        {
            RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites);
        }

        public void GetAllBookmarks()
        {
            DirectoryInfo rootFolder = new DirectoryInfo(RootPath);
            GetShortcutInFolder(rootFolder);
        }

        /// <summary>
        /// Gets the shortcut in folder.
        /// </summary>
        /// <param name="folder">The folder.</param>
        private void GetShortcutInFolder(DirectoryInfo folder)
        {
            IEnumerable<IBookmark> tempList = folder.GetFiles()
                .Where(file => IsUrlShortcut(file.Extension)).Select(file => new BookmarkOfExplorer(file) as IBookmark);
            if(null == BookmarkList)
                BookmarkList = tempList;
            else
                BookmarkList = BookmarkList.Concat(tempList);

            DirectoryInfo[] subFolders = folder.GetDirectories();
            foreach(DirectoryInfo subFolder in subFolders)
                GetShortcutInFolder(subFolder);
        }

        /// <summary>
        /// Determines whether [is URL shortcut] [the specified extension].
        /// </summary>
        /// <param name="extension">The extension.</param>
        /// <returns>
        /// 	<c>true</c> if [is URL shortcut] [the specified extension]; otherwise, <c>false</c>.
        /// </returns>
        private bool IsUrlShortcut(string extension)
        {
            return extension.Equals(".url", StringComparison.OrdinalIgnoreCase);
        }
    }
}
